<?php
/**
 * 首页通知管理
 * author itholiday@126.com
 */
global $_W, $_GPC;
load()->func('tpl');
$html_title = $_W['current_module']['title'];
$op = !empty($_GPC['op']) ? $_GPC['op'] : 'list';
$pagesize = 15;
$page = max(1, $_GPC['page']);
$start = ($page - 1) * $pagesize;

$where = 'id>0 ';
$title = isset($_GPC['title']) && !empty($_GPC['title']) ? input('title') : '';
$start_time = isset($_GPC['start_time']) && !empty($_GPC['start_time']) ? strtotime($_GPC['start_time']) : '';
$end_time = isset($_GPC['end_time']) && !empty($_GPC['end_time']) ? strtotime($_GPC['end_time']) : '';

if (!empty($title)) {
    $where .= " and title like '%$title%' ";
}

if (!empty($start_time) && !empty($end_time)) {
    $where .= " and create_time>=$start_time and create_time<=$end_time ";
} else {
    if (!empty($start_time)) {
        $where .= " and create_time>=$start_time ";
    }
    if (!empty($end_time)) {
        $where .= " and create_time<=$end_time ";
    }
}

if ($op == 'add') {
    if(is_post()){
        $insertData['title'] = trim($_GPC['title']);
        $insertData['content'] = trim($_GPC['content']);
        $insertData['create_time'] = time();
        $res = pdo_insert('yq_haircircle_notice', $insertData);
        if (empty($res)) {
            ajaxError('添加失败');
        }
        ajaxSuccess('添加成功');
    }
    include $this->template('web/notice_add');exit;
}
if ($op == 'ting' && isset($_GPC['yes'])) {
    if (isset($_GPC['id'])) {
        $id = $_GPC['id'];
        $yes = $_GPC['yes'] ? 1 : 0;
        $msg = $yes ? '启用' : '停用';
        $res = pdo_update('yq_haircircle_notice', ['status' => $yes], ['id' => $id]);
        if (!empty($res)) {
            ajaxSuccess($msg . '成功');
        }
        ajaxError($msg . '失败');
    }
    if (isset($_GPC['ids'])) {
        $ids = implode(',', $_GPC['ids']);
        $yes = $_GPC['yes'] ? 1 : 0;
        $msg = $yes ? '启用' : '停用';
        $sql = "update ims_yq_haircircle_notice set status=" . $yes . " where id in ($ids)";
        $res = pdo_query($sql);
        if (!empty($res)) {
            ajaxSuccess($msg . '成功');
        }
        ajaxError($msg . '失败');
    }
}
if ($op == 'del') {
    if (isset($_GPC['id'])) {
        $id = $_GPC['id'];
        $res = pdo_update('yq_haircircle_notice', ['delete_time' => time()], ['id' => $id]);
        if (!empty($res)) {
            ajaxSuccess('删除成功');
        }
        ajaxError('删除失败');
    }
    if (isset($_GPC['ids'])) {
        $ids = implode(',', $_GPC['ids']);
        $sql = "update ims_yq_haircircle_notice set delete_time=" . time() . " where id in ($ids)";
        $res = pdo_query($sql);
        if (!empty($res)) {
            ajaxSuccess('删除成功');
        }
        ajaxError('删除失败');
    }
}

$total = pdo_fetchcolumn('SELECT COUNT(*) AS `number` FROM ' . tablename('yq_haircircle_notice') . ' WHERE ' . $where);
//生成分页HTML
$pager = getPageHTML($total, $page, $pagesize);
$sql = "select * from ims_yq_haircircle_notice where $where and delete_time=0 order by status desc,id desc limit $start,$pagesize";
$list = pdo_fetchall($sql);
$list = empty($list) ? array() : $list;
include $this->template('web/notice_list');
exit;
